package com.x.mvp.utils;

import android.text.Html;
import android.text.Spanned;

import java.util.ArrayList;

/**
 * Created by H on 18/2/28.
 */

public class ColorfullTextUtil {
    private static final String COLORFULL_TEXT = "<font color=\"%1$s\">";
    private static final String COLORFULL_TEXT_END = "%1$s</font>";

    ArrayList<String> htmls = new ArrayList<>();

    public ColorfullTextUtil(String... colors) {
        setColors(colors);
    }

    void setColors(String... colors) {
        if (colors != null) {
            for (String color : colors) {
                htmls.add(String.format(COLORFULL_TEXT, color) + COLORFULL_TEXT_END);
            }
        }
    }

    public String format(String... text) {
        StringBuilder fStr = new StringBuilder();
        if (htmls.size() > 0) {
            for (int i = 0; i < htmls.size(); i++) {
                if (i < text.length) {
                    fStr.append(String.format(htmls.get(i), text[i]));
                }
            }
        }
        return fStr.toString();
    }

    public Spanned formatSpanned(String... text) {
        StringBuilder fStr = new StringBuilder();
        if (htmls.size() > 0) {
            for (int i = 0; i < htmls.size(); i++) {
                if (i < text.length) {
                    fStr.append(String.format(htmls.get(i), text[i]));
                }
            }
        }
        return Html.fromHtml(fStr.toString());
    }
}
